<?php

namespace app\api\model;

use app\api\controller\RongYun;
use think\Model;

class User extends Model
{
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';


    /**
     *
     * 添加好友
     * @param $user_id int 用户id
     * @param $friends_id int 添加好友对象ID
     * @return bool
     * @throws \think\exception\DbException
     */

    public function addFriends($user_id, $friends_id){
        $friends = self::getById($friends_id);
        $param['user_id'] = $user_id;
        $param['friends_id'] = $friends_id;
        if(Friends::get($param))return false;
        if($friends['friends_ver'] == 0){
            //调用融云系统通知消息接口
//            $username = model('user_info')->where('user_id', $user_id)->value('username');
//            $content['content'] = $username.'成功添加您为好友。';
//            $Message['senderId'] = $user_id;
//            $Message['targetId'] = $friends_id;
//            $Message['objectName'] = 'RC:TxtMsg';
//            $Message['content'] = json_encode($content);
//            $ry = new RongYun();
//            $ry_result = $ry->broadcast($Message);
//            if($ry_result['code'] == 200){
                Friends::create($param);
                return true;
//            }
//            return false;
        }
        $data['user_id'] = $friends_id;
        $data['object_id'] = $user_id;
        $data['type'] = 2;//类型  加好友
        $data['status'] = 0;// 申请状态
        Interaction::create($data);
        return true;
    }

    /**
     *
     * 用户余额
     * @param $user_id int  用户ID
     * @param $field  string  余额字段
     * @return mixed
     */
    public function balance($user_id, $field){
        if($field == 'gold_coin' || $field == 'money'){
            return $this->where('id', $user_id)->value($field);
        }
        return false;
    }
}